/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.generate.mybatisplus.handler;

import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.hyts.generate.mybatisplus.bean.AttributeFrameworkBean;
import com.hyts.generate.mybatisplus.bean.AttributePackageBean;
import com.hyts.generate.mybatisplus.bean.GenerateBean;
import com.hyts.generate.mybatisplus.config.DefaultAttributeConfig;
import com.hyts.generate.mybatisplus.handler.base.AbstractAttributeValueHandler;
import com.hyts.generate.mybatisplus.primtive.MpgConfiguration;


import java.util.Objects;
import java.util.Optional;

/**
 * @project-name:standard-boot
 * @package-name:com.hyts.standard.boot.generator.handler
 * @author:LiBo/Alex
 * @create-date:2019-07-26 15:15
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class AttributePackageHandler extends AbstractAttributeValueHandler<AttributePackageBean>{


    /**
     * 构造器
     * @param generateBean
     */
    public AttributePackageHandler(GenerateBean generateBean) {
        super(generateBean);
        Objects.requireNonNull(Objects.isNull(generateBean),"操作服务创建对象不可以为空!");
    }

    /**
     * build构建器
     * @param param
     * @return
     */
    @Override
    public GenerateBean build(AttributePackageBean param) {
        //后台服务校验控制对象
        Optional<PackageConfig> optional = Optional.of(param).map(element->{
            PackageConfig packageConfig = new PackageConfig();
            packageConfig.setParent((String) getValue(param.getParentPackageName(),DefaultAttributeConfig.DEFAULT_PACKAGE_PARENT_PATH));
            packageConfig.setModuleName((String) getValue(param.getModulePackageName(),DefaultAttributeConfig.DEFAULT_MODULE_NAME+"_"+Objects.hash(DefaultAttributeConfig.DEFAULT_MODULE_NAME)));
            packageConfig.setServiceImpl((String) getValue(param.getServiceImplPackageName(),MpgConfiguration.PACKAGE_CONFIG.getServiceImpl()));
            packageConfig.setService((String) getValue(param.getServicePackageName(),MpgConfiguration.PACKAGE_CONFIG.getService()));
            packageConfig.setController((String) getValue(param.getControllerPackageName(),MpgConfiguration.PACKAGE_CONFIG.getController()));
            packageConfig.setEntity((String)getValue(param.getEntityPackageName(),MpgConfiguration.PACKAGE_CONFIG.getEntity()));
            packageConfig.setMapper((String) getValue(param.getMapperPackageName(),MpgConfiguration.PACKAGE_CONFIG.getMapper()));
            packageConfig.setXml((String) getValue(param.getMapperXmlPackageName(),MpgConfiguration.PACKAGE_CONFIG.getXml()));
            return packageConfig;
        });
        return generateBean.setPackageConfig(optional.get());
    }
}
